package com.tianma315.web.wx.controller;


import java.util.Arrays;


import com.tianma315.core.common.utils.Result;
import com.tianma315.core.wx.domain.WxUserInfoDO;
import com.tianma315.core.wx.service.WxUserInfoService;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <pre>
 * 微信用户数据
 * </pre>
 * <small> 2020-02-21 16:31:43 | Aron</small>
 */
@Controller
@RequestMapping("/wx/wxUserInfo")
public class WxUserInfoController extends BaseController {
	@Autowired
	private WxUserInfoService wxUserInfoService;
	
	@GetMapping()
	@RequiresPermissions("wx:wxUserInfo:wxUserInfo")
	String WxUserInfo(){
	    return "wx/wxUserInfo/wxUserInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:wxUserInfo:wxUserInfo")
	public Result<Page<WxUserInfoDO>> list(int pageNumber, int pageSize, String searchKey){
		// 查询列表数据
		Page page = wxUserInfoService.getPage(getMerchantId(), pageNumber, pageSize, searchKey);
		return Result.ok(page);
	}

	/**
	 * 用户区域分布统计视图
	 *
	 * @return
	 */
	@GetMapping("/area")
	ModelAndView userArea() {
		return new ModelAndView("wx/wxUserInfo/user-area");
	}

	
	@GetMapping("/add")
	@RequiresPermissions("wx:wxUserInfo:add")
	String add(){
	    return "wx/wxUserInfo/add";
	}

	@GetMapping("/edit/{wxId}")
	@RequiresPermissions("wx:wxUserInfo:edit")
	String edit(@PathVariable("wxId") Long wxId,Model model){
		WxUserInfoDO wxUserInfo = wxUserInfoService.selectById(wxId);
		model.addAttribute("wxUserInfo", wxUserInfo);
	    return "wx/wxUserInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:wxUserInfo:add")
	public Result<String> save( WxUserInfoDO wxUserInfo){
		if(wxUserInfoService.insert(wxUserInfo)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:wxUserInfo:edit")
	public Result<String>  update( WxUserInfoDO wxUserInfo){
		wxUserInfoService.updateById(wxUserInfo);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:wxUserInfo:remove")
	public Result<String>  remove( Long wxId){
		if(wxUserInfoService.deleteById(wxId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:wxUserInfo:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] wxIds){
		wxUserInfoService.deleteBatchIds(Arrays.asList(wxIds));
		return Result.ok();
	}
	
}
